#include"StringHelper.h"

/**
	Parse TargetStr into ResultArray with SplitStr.
*/
void StringHelper::ParseIntoArray(vector<string> &ResultArray,const string TargetStr,const string SplitStr)
{
	int iSplitCount = 0;

	string TargetCpy = TargetStr;
	string TempStr;
	while(TargetCpy!="")
	{
		TempStr = DoSplit(TargetCpy,SplitStr);
		if(TempStr!="")
		{
			ResultArray.push_back(TempStr);
		}
	}
}

/**
	Help ParseIntoArray() function.	
*/
string StringHelper::DoSplit(string &TargetStr,const string SplitStr)
{
	string strTemp;				//	record chars we viewed
	string strRecord;			//	for check is SplitStr appears

	int iTargetLength = TargetStr.length();
	int iSplitLength = SplitStr.length();

	if(!IsInclude(TargetStr,SplitStr))
	{
		string TargetCpy = TargetStr;
		TargetStr = "";
		return TargetCpy;
	}


	for(int i=0;i<iTargetLength;i++)
	{
		//	record chars we viewed
		strTemp += TargetStr.at(i);

		//	record for check SplitStr
		if(int(strRecord.length()) < iSplitLength)
		{
			strRecord += TargetStr.at(i);
		}
		else
		{
			for(int j=0;j<iSplitLength-1;j++)
			{
				strRecord[j] = strRecord[j+1];
			}
			strRecord[iSplitLength-1] = TargetStr.at(i);
		}

		//	SplitStr appears
		if(strRecord == SplitStr)
		{
			//	we return string before SplitStr ,
			//	modify TargetStr parameter value.

			int iTempLength = int(strTemp.length());
			TargetStr = TargetStr.substr(i+1,iTargetLength - iTempLength);
			strTemp = strTemp.erase(iTempLength-iSplitLength,iSplitLength);

			return strTemp;
		}
	}
	return "";
}


/**
	Check is TargetStr start with CheckStr.
		
	Return true 
		if start with CheckStr.
	Return false
		if not start with CheckStr.
*/
bool StringHelper::IsStartWith(const string TargetStr,const string CheckStr)
{
	int iCheckStrLength = int(CheckStr.length());
	if(iCheckStrLength == 0)
		return false;

	return TargetStr.substr(0,iCheckStrLength) == CheckStr;
}


/**
	Check is TargetStr including CheckStr.

	Return true 
		if it include.
	Return false
		if not include.
*/
bool StringHelper::IsInclude(const string TargetStr,const string CheckStr)
{
	int iTargetLength = TargetStr.length();
	int iCheckLength = CheckStr.length();

	if(iTargetLength < iCheckLength)
		return false;

	for(int i=0;i <= iTargetLength - iCheckLength;i++)
	{
		if(TargetStr.substr(i,iCheckLength) == CheckStr)
		{
			return true;
		}
	}

	return false;
}

/**
	Get string which removed SPACE.
*/
string StringHelper::Trim(string TargetStr)
{
	string result;

	char ch;
	int iStrLength = TargetStr.length();
	
	for(int i=0;i<iStrLength;i++)
	{
		if((ch = TargetStr.at(i))!=' ')
		{
			result += ch;
		}
	}

	return result;
}



/**
	Is TargetStr a number.
	Return true if it's a number.
	Return false if it's not a number.
*/
bool StringHelper::IsNum(const string TargetStr)
{
	int iLength = TargetStr.length();

	if(iLength == 0)
		return false;
	
	char ch;
	for(int i=0;i<iLength;i++)
	{
		ch = TargetStr.at(i);
		if(ch < '0'|| ch > '9')
		{
			if(i==0 && ch == '-')
			{
				if(iLength<2)
					return false;
				continue;
			}
				
			return false;
		}
	}

	return true;
}


/**
	Parse number string into integer.
*/
int StringHelper::ParseToInt(const string TargetStr)
{
	assert(IsNum(TargetStr));

	bool bNegNum = StringHelper::IsStartWith(TargetStr,"-");

	int iLength = TargetStr.length();

	char ch;
	int num = 0,result = 0;
	
	for(int i=bNegNum?1:0;i<iLength;i++)
	{
		ch = TargetStr.at(i);
		num = ch - 48;
		result += int(num * pow(float(10),float(iLength - i - 1)));
	}

	if(bNegNum)
		result = -result;

	return result;
}



/**
	Parse string to LPCWSTR
*/
LPCWSTR StringHelper::ParaseToLPCWSTR(const string TargetStr)
{
	size_t iLength = TargetStr.length() + 1;
	const size_t newsize = 100;
	size_t convetedChars = 0;

	wchar_t *wcstring = (wchar_t*)malloc(sizeof(wchar_t)*(TargetStr.length()-1));
	mbstowcs_s(&convetedChars,wcstring,iLength,TargetStr.c_str(),_TRUNCATE);

	return wcstring;
}